<template>
	<view>
		<u-navbar
		  title="奖金优化"
		  title-color="#fff"
		  title-size="32"
		  :background="{
		    backgroundColor:'#CB3A35'
		  }"
		  back-icon-color="#fff"
		>
		</u-navbar>
		<u-tabs style="margin-top: 10px;" active-color="#f00" :list="list" :is-scroll="false" :current="current" @change="change"></u-tabs>
		<view v-if="current == 0">
			<u-table style="margin-top: 10px;width: 100%;">
					<u-tr style="width: 100%;">
						<view style="width: 20%;"><u-th>过关</u-th></view>
						<view style="width: 34%;"><u-th>单注组合</u-th></view>
						<view style="width: 28%;"><u-th>注数分布</u-th></view>
						<view style="width: 18%;"><u-th>预测奖金</u-th></view>
					</u-tr>
					<view  v-for="(item,index) in newChecks.result" >
						<u-tr>
							<view style="width: 20%;">
								<u-td style="height: 50px;">
									{{listoption[0].name}}
								</u-td>
							</view>
							<view   style="width: 34%;">
								<u-td style="height: 50px;position: relative;padding-top: 10px;" >
									<u-tr style="text-align: center;" v-if="index ==0|| index==1" v-for="(bbs,index) in item.result">
										<span style="font-size: 12px;" v-if="bbs.type =='had.h' " >{{bbs.homeTeamAbbName}}|胜({{bbs.odds}})</span>
										<span style="font-size: 12px;" v-if="bbs.type =='had.d' " >{{bbs.homeTeamAbbName}}|平({{bbs.odds}})</span>
										<span style="font-size: 12px;" v-if="bbs.type =='had.a' " >{{bbs.homeTeamAbbName}}|负({{bbs.odds}})</span>
										<span style="font-size: 12px;" v-if="bbs.type =='hhad.h' " >{{bbs.homeTeamAbbName}}|胜平({{bbs.odds}})</span>
										<span style="font-size: 12px;" v-if="bbs.type =='hhad.d' " >{{bbs.homeTeamAbbName}}|让平({{bbs.odds}})</span>
										<span style="font-size: 12px;" v-if="bbs.type =='hhad.a' " >{{bbs.homeTeamAbbName}}|让负({{bbs.odds}})</span>
									</u-tr>
									<!-- v-if="showtable == false  " -->
									<u-icon
									style="padding-left: 50px;"
									  name="arrow-down"
									  color="#999"
									  @click="showView(showtable,index)"
									  size="16"
									></u-icon>
<!-- 									<u-icon
									v-else
									style="margin: 0 auto;padding-bottom: 0px;cursor: pointer;"
									  name="arrow-up"
									  color="#999"
									  @click="showView(showtable,index)"
									  size="16"
									></u-icon> -->
									</u-td>
							</view>

							<view style="width: 28%;">
								<u-td style="text-align: center;height: 50px;">
									<u-number-box style="width: 80px;height: 5px;margin-left: 4px;" v-model="item.multiply" @change="valChange"></u-number-box>
								</u-td>
							</view>
							<view style="width: 18%;">
								<u-td style="height: 50px;">{{(item.perLottery * item.multiply).toFixed(2)}}</u-td>
							</view>
						</u-tr>
						<u-table v-if="showtable && idx == index">
							<u-tr>
								<u-td>组合方案</u-td>
							</u-tr>
							<u-tr>
								<u-td>场次</u-td>
								<u-td>主队</u-td>
								<u-td>客队</u-td>
								<u-td>投注内容</u-td>
							</u-tr>
							<u-tr v-for="(nn,index) in item.result ">
								<u-td>{{nn.matchNumStr}}</u-td>
								<u-td>{{nn.homeTeamAbbName}}</u-td>
								<u-td>{{nn.awayTeamAbbName}}</u-td>
								<u-td v-if="nn.type =='had.h' ">胜({{nn.odds}})</u-td>
								<u-td v-if="nn.type =='had.d' ">平({{nn.odds}})</u-td>
								<u-td v-if="nn.type =='had.a' ">负({{nn.odds}})</u-td>
								<u-td v-if="nn.type =='hhad.h' ">胜平({{nn.odds}})</u-td>
								<u-td v-if="nn.type =='hhad.d' ">让平({{nn.odds}})</u-td>
								<u-td v-if="nn.type =='hhad.a' ">让负({{nn.odds}})</u-td>
							</u-tr>
						</u-table>
					</view>

				</u-table>

		</view>
		<view v-if="current == 1">
			<u-table style="margin-top: 10px;width: 100%;">
								<u-tr style="width: 100%;">
									<view style="width: 20%;"><u-th>过关</u-th></view>
									<view style="width: 34%;"><u-th>单注组合</u-th></view>
									<view style="width: 28%;"><u-th>注数分布</u-th></view>
									<view style="width: 18%;"><u-th>预测奖金</u-th></view>
								</u-tr>
								<view  v-for="(item,index) in newChecks.result" >
									<u-tr>
										<view style="width: 20%;">
											<u-td style="height: 50px;">
												{{listoption[0].name}}
											</u-td>
										</view>
										<view   style="width: 34%;">
											<u-td style="height: 50px;position: relative;padding-top: 10px;" >
												<u-tr style="text-align: center;" v-if="index ==0|| index==1" v-for="(bbs,index) in item.result">
													<span style="font-size: 12px;" v-if="bbs.type =='had.h' " >{{bbs.homeTeamAbbName}}|胜({{bbs.odds}})</span>
													<span style="font-size: 12px;" v-if="bbs.type =='had.d' " >{{bbs.homeTeamAbbName}}|平({{bbs.odds}})</span>
													<span style="font-size: 12px;" v-if="bbs.type =='had.a' " >{{bbs.homeTeamAbbName}}|负({{bbs.odds}})</span>
													<span style="font-size: 12px;" v-if="bbs.type =='hhad.h' " >{{bbs.homeTeamAbbName}}|胜平({{bbs.odds}})</span>
													<span style="font-size: 12px;" v-if="bbs.type =='hhad.d' " >{{bbs.homeTeamAbbName}}|让平({{bbs.odds}})</span>
													<span style="font-size: 12px;" v-if="bbs.type =='hhad.a' " >{{bbs.homeTeamAbbName}}|让负({{bbs.odds}})</span>
												</u-tr>
												<!-- v-if="showtable == false  " -->
												<u-icon
												style="padding-left: 50px;"
												  name="arrow-down"
												  color="#999"
												  @click="showView(showtable,index)"
												  size="16"
												></u-icon>
												</u-td>
										</view>

										<view style="width: 28%;">
											<u-td style="text-align: center;height: 50px;">
												<u-number-box style="width: 80px;height: 5px;margin-left: 4px;" v-model="item.multiply" @change="valChange"></u-number-box>
											</u-td>
										</view>
										<view style="width: 18%;">
											<u-td style="height: 50px;">{{(item.perLottery * item.multiply).toFixed(2)}}</u-td>
										</view>
									</u-tr>
									<u-table v-if="showtable && idx == index">
										<u-tr>
											<u-td>组合方案</u-td>
										</u-tr>
										<u-tr>
											<u-td>场次</u-td>
											<u-td>主队</u-td>
											<u-td>客队</u-td>
											<u-td>投注内容</u-td>
										</u-tr>
										<u-tr v-for="(nn,index) in item.result ">
											<u-td>{{nn.matchNumStr}}</u-td>
											<u-td>{{nn.homeTeamAbbName}}</u-td>
											<u-td>{{nn.awayTeamAbbName}}</u-td>
											<u-td v-if="nn.type =='had.h' ">胜({{nn.odds}})</u-td>
											<u-td v-if="nn.type =='had.d' ">平({{nn.odds}})</u-td>
											<u-td v-if="nn.type =='had.a' ">负({{nn.odds}})</u-td>
											<u-td v-if="nn.type =='hhad.h' ">胜平({{nn.odds}})</u-td>
											<u-td v-if="nn.type =='hhad.d' ">让平({{nn.odds}})</u-td>
											<u-td v-if="nn.type =='hhad.a' ">让负({{nn.odds}})</u-td>
										</u-tr>
									</u-table>
								</view>

							</u-table>
		</view>
		<view v-if="current == 2">
		<u-table style="margin-top: 10px;width: 100%;">
							<u-tr style="width: 100%;">
								<view style="width: 20%;"><u-th>过关</u-th></view>
								<view style="width: 34%;"><u-th>单注组合</u-th></view>
								<view style="width: 28%;"><u-th>注数分布</u-th></view>
								<view style="width: 18%;"><u-th>预测奖金</u-th></view>
							</u-tr>
							<view  v-for="(item,index) in newChecks.result" >
								<u-tr>
									<view style="width: 20%;">
										<u-td style="height: 50px;">
											{{listoption[0].name}}
										</u-td>
									</view>
									<view   style="width: 34%;">
										<u-td style="height: 50px;position: relative;padding-top: 10px;" >
											<u-tr style="text-align: center;" v-if="index ==0|| index==1" v-for="(bbs,index) in item.result">
												<span style="font-size: 12px;" v-if="bbs.type =='had.h' " >{{bbs.homeTeamAbbName}}|胜({{bbs.odds}})</span>
												<span style="font-size: 12px;" v-if="bbs.type =='had.d' " >{{bbs.homeTeamAbbName}}|平({{bbs.odds}})</span>
												<span style="font-size: 12px;" v-if="bbs.type =='had.a' " >{{bbs.homeTeamAbbName}}|负({{bbs.odds}})</span>
												<span style="font-size: 12px;" v-if="bbs.type =='hhad.h' " >{{bbs.homeTeamAbbName}}|胜平({{bbs.odds}})</span>
												<span style="font-size: 12px;" v-if="bbs.type =='hhad.d' " >{{bbs.homeTeamAbbName}}|让平({{bbs.odds}})</span>
												<span style="font-size: 12px;" v-if="bbs.type =='hhad.a' " >{{bbs.homeTeamAbbName}}|让负({{bbs.odds}})</span>
											</u-tr>
											<!-- v-if="showtable == false  " -->
											<u-icon
											style="padding-left: 50px;"
											  name="arrow-down"
											  color="#999"
											  @click="showView(showtable,index)"
											  size="16"
											></u-icon>
		<!-- 									<u-icon
											v-else
											style="margin: 0 auto;padding-bottom: 0px;cursor: pointer;"
											  name="arrow-up"
											  color="#999"
											  @click="showView(showtable,index)"
											  size="16"
											></u-icon> -->
											</u-td>
									</view>

									<view style="width: 28%;">
										<u-td style="text-align: center;height: 50px;">
											<u-number-box style="width: 80px;height: 5px;margin-left: 4px;" v-model="item.multiply" @change="valChange"></u-number-box>
										</u-td>
									</view>
									<view style="width: 18%;">
										<u-td style="height: 50px;">{{(item.perLottery * item.multiply).toFixed(2)}}</u-td>
									</view>
								</u-tr>
								<u-table v-if="showtable && idx == index">
									<u-tr>
										<u-td>组合方案</u-td>
									</u-tr>
									<u-tr>
										<u-td>场次</u-td>
										<u-td>主队</u-td>
										<u-td>客队</u-td>
										<u-td>投注内容</u-td>
									</u-tr>
									<u-tr v-for="(nn,index) in item.result ">
										<u-td>{{nn.matchNumStr}}</u-td>
										<u-td>{{nn.homeTeamAbbName}}</u-td>
										<u-td>{{nn.awayTeamAbbName}}</u-td>
										<u-td v-if="nn.type =='had.h' ">胜({{nn.odds}})</u-td>
										<u-td v-if="nn.type =='had.d' ">平({{nn.odds}})</u-td>
										<u-td v-if="nn.type =='had.a' ">负({{nn.odds}})</u-td>
										<u-td v-if="nn.type =='hhad.h' ">胜平({{nn.odds}})</u-td>
										<u-td v-if="nn.type =='hhad.d' ">让平({{nn.odds}})</u-td>
										<u-td v-if="nn.type =='hhad.a' ">让负({{nn.odds}})</u-td>
									</u-tr>
								</u-table>
							</view>

						</u-table>
		</view>
		<view style="position: fixed;bottom: 80px;height: 30px;background: #FFFFFF;width: 100%;text-align: center;line-height: 30px;">计划购买
		<u-number-box :min="2" :disabled-input = "true" :step="2" style="margin-left:16px;" v-model="totalMoney" @change="valChange" @minus="chooseMaxSub(newChecks)" @plus="chooseMinAdd(newChecks)"></u-number-box>元
		</view>
		<view style="position: fixed;bottom: 50px;height: 30px;background: #FFFFFF;width: 100%;text-align: center;border-top: 1px solid #eee;line-height: 30px;">共{{totalMoney}}元&nbsp;&nbsp;预计奖金{{minzhi}}~{{maxzhi}}</view>
		<view @click="handleConfirmxiangqing"  style="position: fixed;bottom: 0;height: 50px;width: 100%;background: #f00;color: #FFFFFF;font-size: 17px;text-align: center;line-height: 50px;" >确认购买</view>
		<!-- <view @click="arrangeMoney(optimizeType,req)">
        (点击测试函数)
      </view> -->
	</view>
</template>

<script>
import Decimal from 'decimal.js';
	export default {
		data() {
			return {
				list: [{
					name: '奖金平局'
				}, {
					name: '博热保本'
				}, {
					name: '奖金最高',

				}],
				newChecks:'',
				current: 0,
				chuanguan:'',//
				value: 0,
				// valueradio:'二串一',
				listoption: [
								{
									name: '二串一',

								}
							],
				showtable:false,
				idx:0,
				totalMoney:0,
				totalMoneyCar:0,
				minLottery:0,
				maxLottery:0,
				minzhi:0,
				maxzhi:0,
				arrMaxMin:[],
				mutarray:[],
				mutMin:0,
				needData: `
        {
	"betAmount": 64,
	"minLottery": "91.85",
	"maxLottery": "173.60",
	"multiply": 8,
	"betCnt": 4,
	"betType": "几串几",
	"result": [{

"result": [{

"type": "had.h",

"odds": 2.76,

"matchId": 1008500,

"matchNumStr": "周六001",

"homeTeamAbbName": "大阪钢巴",

"awayTeamAbbName": "东京FC"

}, {

"type": "hhad.h",

"odds": 2.08,

"matchId": 1008501,

"matchNumStr": "周六002",

"homeTeamAbbName": "广岛三箭",

"awayTeamAbbName": "川崎前锋"

}],

"perLottery": "11.48",

"multiply": 8
	}, {

"result": [{

"type": "had.h",

"odds": 2.76,

"matchId": 1008500,

"matchNumStr": "周六001",

"homeTeamAbbName": "大阪钢巴",

"awayTeamAbbName": "东京FC"

}, {

"type": "hhad.d",

"odds": 3.5,

"matchId": 1008501,

"matchNumStr": "周六002",

"homeTeamAbbName": "广岛三箭",

"awayTeamAbbName": "川崎前锋"

}],

"perLottery": "19.32",

"multiply": 8
	}, {

"result": [{

"type": "had.d",

"odds": 3.1,

"matchId": 1008500,

"matchNumStr": "周六001",

"homeTeamAbbName": "大阪钢巴",

"awayTeamAbbName": "东京FC"

}, {

"type": "hhad.h",

"odds": 2.08,

"matchId": 1008501,

"matchNumStr": "周六002",

"homeTeamAbbName": "广岛三箭",

"awayTeamAbbName": "川崎前锋"

}],

"perLottery": "12.90",

"multiply": 8
	}, {

"result": [{

"type": "had.d",

"odds": 3.1,

"matchId": 1008500,

"matchNumStr": "周六001",

"homeTeamAbbName": "大阪钢巴",

"awayTeamAbbName": "东京FC"

}, {

"type": "hhad.d",

"odds": 3.5,

"matchId": 1008501,

"matchNumStr": "周六002",

"homeTeamAbbName": "广岛三箭",

"awayTeamAbbName": "川崎前锋"

}],

"perLottery": "21.70",

"multiply": 8
	}]
}
        `,
            	optimizeType: 2, //入参 1:平均优化 2:博热优化 3:博冷优化

            	req: {}, //入参 数据结构



			}
		},
		watch: { // 监听数组改动
			'newChecks.result': {
				handler(val) {
					this.arrMaxMin=[]
					if (val.length > 0) {
						let count = 0
						val.forEach((item) => {
							this.arrMaxMin.push((item.multiply * item.perLottery).toFixed(2))
							count += Number(item.multiply)


						})
						this.maxzhi =Math.max.apply(null,this.arrMaxMin)
						this.minzhi =Math.min.apply(null,this.arrMaxMin)
						console.log(this.maxzhi,this.minzhi,"adgfeweret")
						this.totalMoney = count * 2

					}
				},
				deep: true,

			},
			totalMoney:{
				handler(val,val1){
					this.mutarray=[]
					// val新值val旧值
					let count = 0
					this.newChecks.result.forEach((item) => {
						this.arrMaxMin.push((item.multiply * item.perLottery).toFixed(2))
						count += Number(item.multiply)
						this.mutarray.push(item.multiply)
						console.log(item.multiply,"d1111111111")
						console.log(val,val1,"gdgdkljgldsjgldsj")
					})
				}
			}
		},

		methods: {
			change(index) {
				this.current = index;
				this.arrangeMoney(index+1,this.newChecks)


			},
			valChange(e) {
							// console.log('当前值为: ' +e.val)
							//联动
							// this.totalMoneyCar =
						},
			showView(val,index){

				this.showtable = !val
				this.idx = index
			},
			handleConfirmxiangqing() {
				console.log('aaadsad', this.newChecks)
				if (this.totalMoney < 10) {
				uni.showToast({
					title: '投注金额必须大于10元',
					icon: 'none',
					duration: 1000
				});
				return false
				}
				this.newChecks.time = new Date()
				console.log('local', localStorage.getItem('cht'))
				if (localStorage.getItem('cht')) {
					this.newChecks.betType = localStorage.getItem('cht')
				}
				this.newChecks.betAmount = this.totalMoney
				this.newChecks.minLottery = this.minzhi
				this.newChecks.maxLottery = this.maxzhi			
				const query = encodeURIComponent(JSON.stringify(this.newChecks));
				uni.navigateTo({
					url: `/pages/bet-list/xiangqing?query=${query}`,
				});
			},

			arrangeMoney(type, req) {
				let amountLottery = 0; //总奖金
				let averageLottery = 0; //平均奖金
				let len = req.result.length; //数组长度
				let totalMultiply = len*req.multiply;
				let tmp = 0
				let tmpLottery =0
				let tmpMul = 0
				let minLottery = Number.MAX_VALUE;
				let maxLottery = 0
				let halfLottery = 0
				let leftAverageLottery = 0
				switch (type) {
					case 1:
						req.result.forEach((v, i) => {
							tmp+= (new Decimal(v.perLottery)).toNumber()
						})
						amountLottery = (req.multiply)*tmp
						console.log("总奖金",amountLottery,"单注总奖金",tmp)
						averageLottery = new Decimal(amountLottery).div(new Decimal(len)).toNumber()
						console.log(averageLottery)
						req.result.forEach((v, i) => {
							if (i == len - 1) {
								req.result[i].multiply = totalMultiply - tmpMul
								return
							}
							tmp = new Decimal(averageLottery).div(v.perLottery).toNumber()
							req.result[i].multiply = parseInt(tmp)
							tmpMul += parseInt(tmp)
						})
						break
					case 2:
						req.result.forEach((v, i) => {
							tmp+= (new Decimal(v.perLottery)).toNumber()

							tmpLottery = v.perLottery
							if (tmpLottery < minLottery) {
								minLottery = tmpLottery
							}
						})
						amountLottery = (req.multiply)*tmp
						halfLottery = new Decimal(amountLottery).div(new Decimal(2)).toNumber()

						leftAverageLottery = new Decimal(halfLottery).div(new Decimal(len)).toNumber()
						req.result.forEach((v, i) => {
							if (i == len - 1) {
								req.result[i].multiply = totalMultiply - tmpMul
								return
							}
							if (v.perLottery == minLottery) {
								//博热 总奖金的50%
								tmp = parseInt(new Decimal(halfLottery).div(v.perLottery).toNumber())
								tmpMul += tmp
								req.result[i].multiply = tmp
							} else {
								tmp = new Decimal(leftAverageLottery).div(v.perLottery).toNumber()
								req.result[i].multiply = parseInt(tmp)
								tmpMul += parseInt(tmp)
							}
						})
						break
					case 3:
						req.result.forEach((v, i) => {
							tmp+= (new Decimal(v.perLottery)).toNumber()

							tmpLottery = v.perLottery
							if (tmpLottery > maxLottery) {
								maxLottery = tmpLottery
							}
						})
						amountLottery = (req.multiply)*tmp
						halfLottery = new Decimal(amountLottery).div(new Decimal(2)).toNumber()

						leftAverageLottery = new Decimal(halfLottery).div(new Decimal(len)).toNumber()
						req.result.forEach((v, i) => {
							if (i == len - 1) {
								req.result[i].multiply = totalMultiply - tmpMul
								return
							}
							if (v.perLottery == maxLottery) {
								//博热 总奖金的50%
								tmp = parseInt(new Decimal(halfLottery).div(v.perLottery).toNumber())
								tmpMul += tmp
								req.result[i].multiply = tmp
							} else {
								tmp = new Decimal(leftAverageLottery).div(v.perLottery).toNumber()
								req.result[i].multiply = parseInt(tmp)
								tmpMul += parseInt(tmp)
							}
						})
						break

				}
				console.log('req.result', type, req)
				this.newChecks = req
				this.$forceUpdate()
			},

			chooseMinAdd(req){
				let tmpLottery = 0
				let minLottery = Number.MAX_VALUE
				req.result.forEach((v, i) => {
					tmpLottery = new Decimal(v.perLottery).mul(new Decimal(v.multiply)).toNumber()
					if (tmpLottery < minLottery) {
						minLottery = tmpLottery
					}
				})

				console.log("最小值是",minLottery)

				req.result.forEach((v, i) => {
					if (new Decimal(v.perLottery).mul(new Decimal(v.multiply)).toNumber() ==minLottery ) {
						console.log("进来了")
						req.result[i].multiply ++
					}
				})
			},
			chooseMaxSub(req){
				let tmpLottery = 0
				let maxLottery = 0
				req.result.forEach((v, i) => {
					tmpLottery = new Decimal(v.perLottery).mul(new Decimal(v.multiply)).toNumber()
					if (tmpLottery > maxLottery) {
						maxLottery = tmpLottery
					}
				})

				console.log("最小值是",maxLottery)

				req.result.forEach((v, i) => {
					if (new Decimal(v.perLottery).mul(new Decimal(v.multiply)).toNumber() ==maxLottery ) {
						req.result[i].multiply --
					}
				})
			}

		},
		onLoad({ query }) { 
			this.newChecks = JSON.parse(decodeURIComponent(query));
			console.log(this.newChecks,"4555555555555")
			this.arrangeMoney(1,this.newChecks)
			console.log(this.newChecks,"ssssssssssf")
			this.totalMoney= this.newChecks.betAmount
			this.minLottery = this.newChecks.minLottery
			this.maxLottery = this.newChecks.maxLottery
			for(var i=0;i<this.newChecks.result[0].result.length;i++){
			 if(this.newChecks.result[0].result.length == 1){
				this.listoption[0].name = '单关'
			 }else if(this.newChecks.result[0].result.length == 2){
				this.listoption[0].name = '二串一'
			 }else if(this.newChecks.result[0].result.length == 3){
				this.listoption[0].name = '三串一'
			 }else if(this.newChecks.result[0].result.length == 4){
				 this.listoption[0].name = '四串一'
			 }else if(this.newChecks.result[0].result.length == 5){
			 this.listoption[0].name = '五串一'
			 }else if(this.newChecks.result[0].result.length == 6){
				this.listoption[0].name = '六串一'
			 }else if(this.newChecks.result[0].result.length == 7){
				this.listoption[0].name = '七串一'
			 }else[
				this.listoption[0].name = '八串一'
			 ]
	
			}
			this.$forceUpdate()
		},
	}
</script>

<style>
</style>
